package co.yixiang.modules.member.serviceimpl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import co.yixiang.common.exception.ServiceException;
import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.utils.SecurityUtils;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.member.entity.domain.UserAddress;
import co.yixiang.modules.member.entity.vo.UserAddressVo;
import co.yixiang.modules.member.mapper.UserAddressMapper;
import co.yixiang.modules.member.service.UserAddressService;
import co.yixiang.modules.security.entity.vo.AuthUser;
import co.yixiang.modules.system.entity.domain.User;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class UserAddressServiceImpl extends BaseServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService {
    @Autowired
    private IGenerator generator;

    @Override
    public List<UserAddressVo> queryListByVo() {
        final AuthUser authUser = SecurityUtils.getAuthUser();
        List<UserAddress> list = list(new LambdaQueryWrapper<UserAddress>().eq(UserAddress::getUid, authUser.getUuid()).orderByDesc(UserAddress::getIsDefault).orderByDesc(UserAddress::getCreateTime));
        return generator.convert(list, UserAddressVo.class);
    }

    @Override
    public UserAddressVo createAddress(UserAddress address) {
        final AuthUser authUser = SecurityUtils.getAuthUser();
        address.setUid(authUser.getUuid());
        save(address);
        final UserAddressVo userAddressVo = BeanUtil.copyProperties(address, UserAddressVo.class);
        return userAddressVo;
    }

    /**
     * 根据用户uid获取默认地址信息
     *
     * @return 当前会员的默认地址信息
     */
    @Override
    public UserAddressVo getDefaultMemberAddress() {
        // Objects.requireNonNull(UserContext.getCurrentUser()).getId()
        final AuthUser authUser = SecurityUtils.getAuthUser();
        final UserAddress one = this.getOne(
                new QueryWrapper<UserAddress>()
                        .eq("uid", authUser.getUuid())
                        .eq("is_default", 1));
        final UserAddressVo userAddressVo = BeanUtil.copyProperties(one, UserAddressVo.class);
        return userAddressVo;
    }

    @Override
    public UserAddress getDefaultAddress() {
        final AuthUser authUser = SecurityUtils.getAuthUser();
        final UserAddress one = this.getOne(
                new QueryWrapper<UserAddress>()
                        .eq("uid", authUser.getUuid())
                        .eq("is_default", 1));
        return one;
    }

    @Override
    public UserAddressVo setDefaultMemberAddress(Long id) {
        final UserAddress byId = this.getById(id);
        if (ObjectUtil.isEmpty(byId)) {
            throw new ServiceException("ID为:"+id+"地址信息不错在");
        }
        final AuthUser authUser = SecurityUtils.getAuthUser();
        this.update(new LambdaUpdateWrapper<UserAddress>().set(UserAddress::getIsDefault, 0).eq(UserAddress::getUid,authUser.getUuid()));
        this.update(new LambdaUpdateWrapper<UserAddress>().set(UserAddress::getIsDefault, 1).eq(UserAddress::getUid,authUser.getUuid()).eq(UserAddress::getId,id));
        byId.setIsDefault(1);
        final UserAddressVo userAddressVo = BeanUtil.copyProperties(byId, UserAddressVo.class);
        return userAddressVo;
    }

    @Override
    public UserAddressVo saveOrUpdateAddress(UserAddress address) {
        final AuthUser authUser = SecurityUtils.getAuthUser();
        if (ObjectUtil.isNotEmpty(address.getIsDefault()) && address.getIsDefault() == 1) {
            this.update(new LambdaUpdateWrapper<UserAddress>().set(UserAddress::getIsDefault, 0).eq(UserAddress::getUid,authUser.getUuid()));
        }
        address.setUid(authUser.getUuid());
        this.updateById(address);
        final UserAddressVo userAddressVo = BeanUtil.copyProperties(address, UserAddressVo.class);
        return userAddressVo;
    }

    @Override
    public UserAddressVo saveAddress(UserAddress address) {
        final AuthUser authUser = SecurityUtils.getAuthUser();
        address.setUid(authUser.getUuid());
        this.save(address);
        final UserAddressVo userAddressVo = BeanUtil.copyProperties(address, UserAddressVo.class);
        return userAddressVo;
    }
}
